// pages/group_forward/group_forward.js
var common = require("../../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        choose: 1,
        money: 0,
        is_leader: 1,
        tmplIds: [],
        subscribeMessage: true
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        var money = options.money ? options.money : 0;
        var is_leader = options.is_leader ? options.is_leader : 1;
        //如果用户提现，默认只有余额
                if (is_leader == 0) {
            this.setData({
                choose: 0
            });
        }
        this.setData({
            money: money,
            is_leader: is_leader
        });
        this.getTempIds();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    // onShareAppMessage: function () {
    // },
    //选择提现方式
    choose: function(e) {
        var id = e.currentTarget.dataset.id;
        this.setData({
            choose: id
        });
    },
    //提现输入框
    putmoney: function(e) {
        let money = e.detail.value;
        money = money.replace(/[^\d.]/g, "").replace(/\.{2,}/g, ".").replace(".", "$#$").replace(/\./g, "").replace("$#$", ".").replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3").replace(/^\./g, "");
        this.setData({
            put_forward: money
        });
    },
    //提现所有
    all: function(e) {
        this.setData({
            put_forward: e.currentTarget.dataset.money
        });
    },
    putForward: function() {
        let that = this;
        that.putForward_ch();
        if (that.data.subscribeMessage) {
            wx.requestSubscribeMessage({
                tmplIds: that.data.tmplIds,
                success(res) {},
                fail(err) {
                    console.log(err);
                }
            });
        }
    },
    //修改成点击提交
    putForward_ch: function() {
        var openid = wx.getStorageSync("user").openid;
        var that = this;
        //禁用提现按钮
                that.setData({
            putForward: true
        });
        //提现金额
                var put_forward = that.data.put_forward;
        //提现方式 id
                var type = that.data.choose;
        //总计金额
                var all_money = that.data.money;
        if (parseFloat(put_forward) > parseFloat(all_money)) {
            wx.showModal({
                title: "提示",
                content: "提现金额超出可提现佣金！"
            });
            //解禁提现按钮
                        that.setData({
                putForward: false
            });
            return false;
        }
        common.request({
            endpoint: "Apply/financeapply/broker_submit",
            method: "post",
            data: {
                channel: 1,
                //淘宝客提现
                openid: openid,
                price: put_forward,
                account: type == 0 ? "余额" : "微信",
                type: type,
                //账户类型  0为余额
                channel: 1,
                identity: that.data.is_leader
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                wx.showToast({
                    title: res.message,
                    icon: "none"
                });
                setTimeout(() => {
                    wx.navigateBack({});
                }, 1500);
            },
            fail: res => {
                console.log(res);
                wx.showModal({
                    title: "提示",
                    content: res.message
                });
                //解禁提现按钮
                                that.setData({
                    putForward: false
                });
            }
        });
    },
    //获取tempIds
    getTempIds: function() {
        var that = this;
        common.request({
            endpoint: "set/template_get",
            method: "post",
            data: {},
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                let tmplIds = [];
                let subscribeMessage = true;
                res.extra.map(v => {
                    if (v.type == "26" && v.status == "1") {
                        tmplIds.push(v.template_id);
                    }
                });
                if (!tmplIds.length) {
                    subscribeMessage = false;
                }
                that.setData({
                    tmplIds: tmplIds,
                    subscribeMessage: subscribeMessage
                });
            }
        });
    }
});